home *** CD-ROM | disk | FTP | other *** search
/ C/C++ Users Group Library 1996 July / C-C++ Users Group Library July 1996.iso / vol_100 / 138_01 / gpr.c < prev    next >
Text File  |  1985-03-09  |  3KB  |  169 lines

  1. /*
  2.     gpr.c                created: 01-Oct-83
  3.  
  4.     general purpose utility routines for use with C to simplify
  5.     user/program interaction.
  6.  
  7.     by Anthony Skjellum
  8.  
  9.     Copyright 1983, 1984 (c) Caltech.  All rights reserved.
  10.     This subroutine library may be distributed freely and
  11.     used for all non-commercial purposes but may not be sold.
  12.  
  13.     Routines:
  14.  
  15.         iinp():  integer input w/ prompt, range check + retry
  16.         finp():  float   input w/ prompt, range check + retry
  17.         sinp():  string  input w/ prompt
  18.         cinq():  yes/no question processor w/ prompt + retry
  19.         display(): display a file 
  20.  
  21.     updated: 20-Jul-84
  22.  
  23.     testing information: this code was tested with Aztec C 1.05j
  24. */
  25.  
  26. #include <stdio.h>
  27.  
  28. /* Unix flag (used by display()) */
  29. /*
  30. #define UNIX 1        
  31. */
  32. #ifndef UNIX
  33. #define TEOF    26    /* ^Z */
  34. #endif
  35.  
  36. /* general purpose subroutines: */
  37.  
  38. /* iinp(): integer input with range checking, prompt and retry */
  39.  
  40. iinp(prompt,cflag,low,high)
  41. char *prompt;        
  42. char cflag;
  43. int low;
  44. int high;
  45. {
  46.     int ival;
  47.  
  48.     while(1)
  49.     {
  50.         printf("%s",prompt);
  51.         if(scanf("%d",&ival) < 1)
  52.             while(getchar() != '\n')
  53.                 ;
  54.  
  55.         if((!cflag)||(ival >= low)&&(ival <= high)) 
  56.             break;
  57.                 /* no checking, or within bounds */
  58.  
  59.         printf("\nValue out of range, try again...\n");
  60.     }
  61.  
  62.     return(ival); /* return the value */
  63.  
  64. } /* end iinp() */
  65.  
  66.  
  67. /* finp(): floating point input with range checking, prompt and retry */
  68.  
  69. double finp(prompt,cflag,low,high)
  70. char *prompt;
  71. char cflag;
  72. double low;
  73. double high;
  74. {
  75.     double fval;
  76.  
  77.     while(1)
  78.     {
  79.         printf("%s",prompt);
  80.         while(scanf("%lf",&fval) < 1)
  81.             while(getchar() != '\n')
  82.                 ;
  83.  
  84.         if((!cflag)||(fval >= low)&&(fval <= high)) 
  85.             break;
  86.                 /* no checking, or within bounds */
  87.  
  88.         printf("\nValue out of range, try again...\n");
  89.     }
  90.  
  91.     return(fval); /* return the value */
  92.  
  93. } /* end finp() */
  94.  
  95. /* subroutine sinp(): input a string with prompt, length limit */
  96.  
  97. sinp(prompt,string,length)
  98. char *prompt;
  99. char *string;
  100. int length;
  101. {
  102.     int len;            /* length of actual string input */
  103.     char chr;
  104.     char *fgets();            /* string input function */
  105.  
  106.     printf("%s",prompt);            /* display the prompt */
  107.     while(isspace(chr = getchar()))
  108.         ;
  109.     ungetc(chr,stdin);
  110.     fgets(string,length,stdin);    /* input the string */    
  111.     
  112.     if((len = strlen(string)))
  113.         string[strlen(string)-1] = '\0';
  114.  
  115.     return(len);
  116. } /* end sinp() */
  117.  
  118. /* subroutine cinq(): yes no question processor with prompt, retry  */
  119.  
  120. cinq(prompt)
  121. char *prompt;
  122. {
  123.     char chr;
  124.  
  125.     while(1)
  126.     {
  127.         printf("%s",prompt);
  128.  
  129.         do /* drain spurious 'white space' */
  130.         {
  131.             chr = tolower(getchar()); /* use first char */
  132.         } 
  133.         while(isspace(chr));
  134.  
  135.         if((chr == 'y')||(chr == 'n')) break;
  136.  
  137.         printf("\nRespond with Y or N, please try again...\n");
  138.     }
  139.  
  140.     return((chr == 'y') ? 1 : 0);
  141. }
  142.  
  143.  
  144. /* display(): subroutine to print an ascii file on the console */
  145.  
  146. display(fname)
  147. char *fname;
  148. {
  149.     char c;        /* character to output */
  150.     FILE *disp;
  151.  
  152.     if((disp = fopen(fname,"r")) == NULL)
  153.         return(-1);    /* can't open file */
  154.  
  155.     while((c = getc(disp)) != EOF) /* print the file */
  156.     {
  157. #ifndef UNIX
  158.         if(c == TEOF) /* text end of file */
  159.             break;
  160. #endif
  161.     
  162.         putchar((c & 127));    /* output each character less parity */
  163.     }
  164.  
  165.     fclose(disp);    /* close the file */
  166.     return(0);    /* successful completion */
  167. }
  168.  
  169.